
package com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessContext;
import com.hlkj.pay.app.merchant.ext.impl.MerchantAccessFile;
import com.hlkj.pay.app.merchant.ext.impl.huifu.utils.HuiFuUtil;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.merchant.MerchantAccessScene;
import com.hlkj.pay.dto.merchant.ProviderChannelFeeConfigList;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.*;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.huifu.bspay.sdk.opps.core.request.V2MerchantBasicdataEntRequest;
import com.huifu.bspay.sdk.opps.core.request.V2MerchantBasicdataModifyRequest;
import com.huifu.bspay.sdk.opps.core.utils.DateTools;
import com.huifu.bspay.sdk.opps.core.utils.SequenceTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 汇付 商户进件参数处理
 * 
 * @author HlpayTeam
 * @date 2024/09/26 12:02
 */
@Slf4j
public class HuiFuMerchantAccessCompanyParamsHandle extends HuiFuMerchantAccessCommonParamsHandle {

    /**
     * 企业商户进件-新增参数组装
     *
     * @param channelMerchantAccessContext
     * @return
     */
    public static V2MerchantBasicdataEntRequest v2UserBasicdataEntRequest(ChannelMerchantAccessContext channelMerchantAccessContext) {
        V2MerchantBasicdataEntRequest request = new V2MerchantBasicdataEntRequest();
        String reqSeqId32 = SequenceTools.getReqSeqId32();
        channelMerchantAccessContext.setReqSeqId(reqSeqId32);
        // 请求流水号
        request.setReqSeqId(reqSeqId32);
        // 请求日期
        request.setReqDate(DateTools.getCurrentDateYYYYMMDD());
        MerchantAccessInfoDO merchantAccessInfoDO = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessInfo();
        MerchantAccessLicenseDO merchantAccessLicenseDO = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessLicense();
        MerchantAccessCertInfoDO merchantAccessLegal = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessLegal();
        MerchantAccessCertInfoDO merchantAccessContact = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessContact();
        MerchantAccessBusinessDO merchantAccessBusinessDO = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessBusiness();
        // ----- 商户信息-----
        // 企业用户名称
        request.setRegName(merchantAccessInfoDO.getName());
        // 企业用户名称
        request.setReceiptName(merchantAccessBusinessDO.getShortName());
        // 经营简称
        request.setShortName(merchantAccessBusinessDO.getShortName());
        // 公司类型
        request.setEntType(companyType(merchantAccessLicenseDO.getCompanyType()));
        // 所属行业
        request.setMcc(merchantAccessBusinessDO.getMcc());
        // 经营类型 1：实体，2：虚拟 ；
        request.setBusiType(busiType(merchantAccessBusinessDO.getSceneType()));
        // 联系人姓名
        request.setContactName(merchantAccessContact.getName());
        // 联系人手机号
        request.setContactMobileNo(merchantAccessContact.getPhone());
        // 联系人电子邮箱
        request.setContactEmail(merchantAccessContact.getOtherValue());

        // ----- 营业执照信息-----
        // 营业执照编号
        request.setLicenseCode(merchantAccessLicenseDO.getLicenseCode());
        // 证照有效期类型
        request.setLicenseValidityType(merchantAccessLicenseDO.getLicenseValidityType().toString());
        // 证照有效期起始日期
        request.setLicenseBeginDate(HuiFuUtil.date(merchantAccessLicenseDO.getLicenseBeginDate()));
        // 证照有效期结束日期日期格式：yyyyMMdd; 非长期有效时必填；&lt;font
        // color&#x3D;&quot;green&quot;&gt;示例值：20320905&lt;/font&gt;
        request.setLicenseEndDate(HuiFuUtil.date(merchantAccessLicenseDO.getLicenseEndDate()));
        // 注册地址(省)
        request.setRegProvId(merchantAccessLicenseDO.getProvinceCode());
        // 注册地址(市)
        request.setRegAreaId(merchantAccessLicenseDO.getCityCode());
        // 注册地址(区)
        request.setRegDistrictId(merchantAccessLicenseDO.getAreaCode());
        // 注册地址(详细信息)
        request.setRegDetail(merchantAccessLicenseDO.getAddress());

        // ----- 法人信息-----
        // 法人姓名
        request.setLegalName(merchantAccessLegal.getName());
        // 法人证件类型
        request.setLegalCertType(certType(merchantAccessLegal.getCertType()));
        // 法人证件号码
        request.setLegalCertNo(merchantAccessLegal.getCertNo());
        // 法人证件有效期类型
        request.setLegalCertValidityType(merchantAccessLegal.getCertValidityType().toString());
        // 法人证件有效期开始日期
        request.setLegalCertBeginDate(HuiFuUtil.date(merchantAccessLegal.getCertBeginDate()));
        // 法人证件有效期截止日期日期格式：yyyyMMdd; 非长期有效时必填，长期有效为空；&lt;font
        // color&#x3D;&quot;green&quot;&gt;示例值：20320905&lt;/font&gt;
        request.setLegalCertEndDate(HuiFuUtil.date(merchantAccessLegal.getCertEndDate()));

        // 设置非必填字段
        Map<String, Object> extendInfoMap = getExtendInfos(channelMerchantAccessContext, false);
        request.setExtendInfo(extendInfoMap);
        return request;
    }

    /**
     * 企业商户进件-新增参数组装
     *
     * @param channelMerchantAccessContext
     * @return
     */
    public static V2MerchantBasicdataModifyRequest v2MerchantBusiModify(ChannelMerchantAccessContext channelMerchantAccessContext) {
        V2MerchantBasicdataModifyRequest request = new V2MerchantBasicdataModifyRequest();
        String reqSeqId32 = SequenceTools.getReqSeqId32();
        channelMerchantAccessContext.setReqSeqId(reqSeqId32);
        // 请求流水号
        request.setReqSeqId(reqSeqId32);
        // 请求日期
        request.setReqDate(DateTools.getCurrentDateYYYYMMDD());
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessInfoDO merchantAccessInfoDO = merchantAccessDto.getMerchantAccessInfo();
        MerchantAccessLicenseDO merchantAccessLicenseDO = merchantAccessDto.getMerchantAccessLicense();
        MerchantAccessCertInfoDO merchantAccessLegal = merchantAccessDto.getMerchantAccessLegal();
        MerchantAccessCertInfoDO merchantAccessContact = merchantAccessDto.getMerchantAccessContact();
        MerchantAccessBusinessDO merchantAccessBusinessDO = merchantAccessDto.getMerchantAccessBusiness();

        // 设置非必填字段
        Map<String, Object> extendInfoMap = getExtendInfos(channelMerchantAccessContext, true);
        // 商户信息
        extendInfoMap.put("reg_name", merchantAccessInfoDO.getName());
        extendInfoMap.put("short_name", merchantAccessBusinessDO.getShortName());
        if (merchantAccessLicenseDO != null) {
            extendInfoMap.put("ent_type", companyType(merchantAccessLicenseDO.getCompanyType()));
            extendInfoMap.put("mcc", merchantAccessBusinessDO.getMcc());
            extendInfoMap.put("license_code", merchantAccessLicenseDO.getLicenseCode());
            extendInfoMap.put("license_validity_type", merchantAccessLicenseDO.getLicenseValidityType().toString());
            extendInfoMap.put("license_begin_date", HuiFuUtil.date(merchantAccessLicenseDO.getLicenseBeginDate()));
            extendInfoMap.put("license_end_date", HuiFuUtil.date(merchantAccessLicenseDO.getLicenseEndDate()));
            // 注册地址(省)
            extendInfoMap.put("reg_prov_id", merchantAccessLicenseDO.getProvinceCode());
            // 注册地址(市)
            extendInfoMap.put("reg_area_id", merchantAccessLicenseDO.getCityCode());
            // 注册地址(区)
            extendInfoMap.put("reg_district_id", merchantAccessLicenseDO.getAreaCode());
            // 注册地址(详细信息)
            extendInfoMap.put("reg_detail", merchantAccessLicenseDO.getAddress());
        }

        // 联系人姓名
        extendInfoMap.put("contact_name", merchantAccessContact.getName());
        // 联系人手机号
        extendInfoMap.put("contact_mobile_no", merchantAccessContact.getPhone());
        // 联系人电子邮箱
        extendInfoMap.put("contact_email", merchantAccessContact.getOtherValue());

        // ----- 法人信息-----
        // 法人姓名
        extendInfoMap.put("legal_name", merchantAccessLegal.getName());
        // 法人证件类型
        extendInfoMap.put("legal_cert_type", certType(merchantAccessLegal.getCertType()));
        // 法人证件号码
        extendInfoMap.put("legal_cert_no", merchantAccessLegal.getCertNo());
        // 法人证件有效期类型
        extendInfoMap.put("legal_cert_validity_type", merchantAccessLegal.getCertValidityType().toString());
        // 法人证件有效期开始日期
        extendInfoMap.put("legal_cert_begin_date", HuiFuUtil.date(merchantAccessLegal.getCertBeginDate()));
        // 法人证件有效期截止日期日期格式：yyyyMMdd; 非长期有效时必填，长期有效为空；&lt;font
        // color&#x3D;&quot;green&quot;&gt;示例值：20320905&lt;/font&gt;
        extendInfoMap.put("legal_cert_end_date", HuiFuUtil.date(merchantAccessLegal.getCertEndDate()));

        // 协议信息实体
        extendInfoMap.put("agreement_info", HuiFuMerchantOpenRequestParamsHandle.getAgreementInfo());
        // 签约人
        extendInfoMap.put("sign_user_info", HuiFuMerchantOpenRequestParamsHandle.getSignUserInfo(merchantAccessDto.getMerchantAccessCard(), merchantAccessContact, merchantAccessLegal));
        request.setExtendInfo(extendInfoMap);
        return request;
    }

    /**
     * 非必填字段
     * 
     * @return
     */
    private static Map<String, Object> getExtendInfos(ChannelMerchantAccessContext channelMerchantAccessContext, boolean isUpdate) {
        // 设置非必填字段
        Map<String, Object> extendInfoMap = new HashMap<>();
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();
        MerchantAccessCertInfoDO merchantAccessLegal = merchantAccessDto.getMerchantAccessLegal();
        MerchantAccessLicenseDO merchantAccessLicense = merchantAccessDto.getMerchantAccessLicense();
        // MerchantAccessSceneDO merchantAccessScene =
        // channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessScene();
        MerchantAccessBusinessDO MerchantAccessBusiness = merchantAccessDto.getMerchantAccessBusiness();
        MerchantAccessCardDO merchantAccessCard = merchantAccessDto.getMerchantAccessCard();
        MerchantAccessFile merchantAccessFile = channelMerchantAccessContext.getMerchantAccessFile();
        // 商户信息
        extendInfoMap.put("scene_type", sceneType(MerchantAccessBusiness.getSceneType()));
        extendInfoMap.put("reg_acct_pic", merchantAccessFile.getRegAcctFileId());
        extendInfoMap.put("open_licence_no", merchantAccessCard.getLicenseNo());
        extendInfoMap.put("settle_card_front_pic", merchantAccessFile.getSettleCardFrontFileId());
        extendInfoMap.put("settle_cert_back_pic", merchantAccessFile.getSettleCertBackFileId());
        extendInfoMap.put("settle_cert_front_pic", merchantAccessFile.getSettleCertFrontFileId());
        List<MerchantAccessScene> sceneList = MerchantAccessBusiness.getSceneList();
        if (CollectionUtils.isNotEmpty(sceneList)) {
            List<MerchantAccessScene> webSceneList = sceneList.stream()
                    .filter(merchantAccessScene -> MerchantEnum.MERCHANT_SCENE_TYPE.WEB.getCode().equals(merchantAccessScene.getType())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sceneList)) {
                MerchantAccessScene merchantAccessSceneWeb = webSceneList.get(0);
                if (merchantAccessSceneWeb != null) {
                    extendInfoMap.put("mer_url", merchantAccessSceneWeb.getWebUrl());
                    extendInfoMap.put("mer_icp", merchantAccessSceneWeb.getIcp());
                }
            }
        }

        // 营业执照
        if (merchantAccessLicense != null) {
            extendInfoMap.put("license_pic", merchantAccessFile.getLicensePicFileId());
            extendInfoMap.put("license_type", licenseType(merchantAccessLicense.getLicenseType()));
            extendInfoMap.put("found_date", HuiFuUtil.date(merchantAccessLicense.getRegistrationDate()));
            extendInfoMap.put("reg_capital", HuiFuUtil.date(merchantAccessLicense.getRegisteredCapital()));
            extendInfoMap.put("business_scope", HuiFuUtil.date(merchantAccessLicense.getBusinessScope()));
        }
        // 经营信息
        extendInfoMap.put("prov_id", MerchantAccessBusiness.getProvinceCode());
        extendInfoMap.put("area_id", MerchantAccessBusiness.getCityCode());
        extendInfoMap.put("district_id", MerchantAccessBusiness.getAreaCode());
        extendInfoMap.put("detail_addr", MerchantAccessBusiness.getAddress());
        // 法人信息
        extendInfoMap.put("legal_mobile_no", merchantAccessLegal.getPhone());
        extendInfoMap.put("legal_addr", merchantAccessLegal.getAddress());
        extendInfoMap.put("legal_cert_back_pic", merchantAccessFile.getLegalCertBackFileId());
        extendInfoMap.put("legal_cert_front_pic", merchantAccessFile.getLegalCertFrontFileId());
        // 场景信息
        PayEnums.SCENE_TYPE scene_type = PayEnums.SCENE_TYPE.from(MerchantAccessBusiness.getSceneType());
        switch (scene_type) {
            case OFFLINE:
                extendInfoMap.put("store_header_pic", merchantAccessFile.getStoreHeaderFileId());
                extendInfoMap.put("store_indoor_pic", merchantAccessFile.getStoreIndoorFileId());
                extendInfoMap.put("store_cashier_desk_pic", merchantAccessFile.getStoreCashierDeskFileId());
                break;
            case ONLINE:
                extendInfoMap.put("online_company_pic", merchantAccessFile.getOnlineCompanyFileId());
                extendInfoMap.put("online_company_area_pic", merchantAccessFile.getOnlineCompanyAreaFileId());
                break;
            default:
                extendInfoMap.put("store_header_pic", merchantAccessFile.getStoreHeaderFileId());
                extendInfoMap.put("store_indoor_pic", merchantAccessFile.getStoreIndoorFileId());
                extendInfoMap.put("store_cashier_desk_pic", merchantAccessFile.getStoreCashierDeskFileId());
                extendInfoMap.put("online_company_pic", merchantAccessFile.getOnlineCompanyFileId());
                extendInfoMap.put("online_company_area_pic", merchantAccessFile.getOnlineCompanyAreaFileId());
        }

        extendInfoMap.put("async_return_url", channelMerchantAccessContext.getAsyncReturnUrl());

        MerchantEnum.CARD_TYPE cardType = MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType());
        switch (cardType) {
            case PRIVATE_CORPORATE_ACCOUNT:
                // 对私必填 卡正面
                extendInfoMap.put("settle_card_front_pic", merchantAccessFile.getSettleCardFrontFileId());
                // 对私必填 持卡人身份证国徽面
                extendInfoMap.put("settle_cert_back_pic", merchantAccessFile.getSettleCertBackFileId());
                // 对私必填 持卡人身份证人像面
                extendInfoMap.put("settle_cert_front_pic", merchantAccessFile.getSettleCertFrontFileId());
            case PRIVATE_NON_CORPORATE_ACCOUNT:
                // 对私非法人必填 授权委托书
                extendInfoMap.put("auth_enturst_pic", merchantAccessFile.getAuthEnturstFileId());
                break;
        }
        if (merchantAccessCard != null) {
            // 结算卡信息配置
            extendInfoMap.put("card_info", getCardInfo(merchantAccessCard));
        }

        extendInfoMap.put("settle_config", getSettleConfig(channelMerchantAccessContext.getMerchantFeeConfigList(), isUpdate));

        if (!CollectionUtils.isEmpty(merchantAccessDto.getMerchantAccessBenefitList())) {
            // 受益人规则配置
            extendInfoMap.put("beneficiary_info", getBeneficiaryConfig(merchantAccessDto.getMerchantAccessBenefitList()));
        }
        // 异步请求地址
        extendInfoMap.put("async_return_url", channelMerchantAccessContext.getAsyncReturnUrl());
        return extendInfoMap;
    }

    public static String getSettleConfig(List<ProviderChannelFeeConfigList> merchantFeeConfigList, boolean isUpdate) {
        cn.hutool.json.JSONObject dto = new cn.hutool.json.JSONObject();
        if (isUpdate)
            dto.set("settle_status", "1");
        dto.set("settle_cycle", "D1");
        dto.set("fixed_ratio", "0.05");
        dto.set("constant_amt", "0.00");
        ProviderChannelFeeConfigList feeConfig = merchantFeeConfigList.stream()
                .filter(config -> Objects.equals(PayEnums.PAY_TYPE.FUNCTION.getCode(), config.getPayWayDO().getType())).findFirst().orElse(null);
        if (feeConfig != null) {
            PayWayDO payWayDO = feeConfig.getPayWayDO();
            if (MerchantEnum.SETTLE_CYCLE.T1.getPayWayCode().equals(payWayDO.getCode())) {
                dto.set("settle_cycle", "T1");
            }
            dto.set("fixed_ratio", feeConfig.getDefaultFeeConfigValue().getRateBase().toString());
            if (feeConfig.getDefaultFeeConfigValue().getFixedRate() != null) {
                DecimalFormat df = new DecimalFormat("#0.00");
                dto.set("constant_amt", df.format(feeConfig.getDefaultFeeConfigValue().getFixedRate()));
            }
        }
        return dto.toString();
    }

    private static String getBeneficiaryConfig(List<MerchantAccessCertInfoDO> merchantAccessBenefits) {
        JSONArray JSONArray = new JSONArray();
        merchantAccessBenefits.stream().forEach(merchantAccessBenefitDO -> {
            JSONObject dto = new JSONObject();
            // 受益人名称
            dto.put("bo_name", merchantAccessBenefitDO.getName());
            // 受益人证件类型
            dto.put("bo_type", merchantAccessBenefitDO.getCertType());
            // 受益人证件号
            dto.put("bo_no", merchantAccessBenefitDO.getCertNo());
            // 受益人证件有效期开始时间
            dto.put("bo_date_start", HuiFuUtil.date(merchantAccessBenefitDO.getCertBeginDate()));
            // 受益人证件有效期结束时间
            dto.put("bo_dead_line", HuiFuUtil.date(merchantAccessBenefitDO.getCertEndDate()));
            // 受益人证件地址
            dto.put("bo_address", merchantAccessBenefitDO.getAddress());
            // 受益人手机号
            dto.put("bo_mobile_no", merchantAccessBenefitDO.getPhone());
            // 最终受益人受益方式
            dto.put("final_beneficiary_mode", merchantAccessBenefitDO.getOtherValue());
            JSONArray.add(dto);
        });
        return JSONArray.toJSONString();
    }

    // public static String busiType(Integer sceneType) {
    // PayEnums.SCENE_TYPE scene_type = PayEnums.SCENE_TYPE.from(sceneType);
    // switch (scene_type) {
    // case ONLINE:
    // return "2";
    // case OFFLINE:
    // return "1";
    // default:
    // return "1";
    // }
    // }

    // private static String getCardInfo(MerchantAccessCardDO merchantAccessCardDO)
    // {
    // JSONObject dto = new JSONObject();
    // // 结算账户类型
    // dto.put("card_type", merchantAccessCardDO.getType());
    // // 结算账户名
    // dto.put("card_name", merchantAccessCardDO.getName());
    // // 结算账号
    // dto.put("card_no", merchantAccessCardDO.getCardNo());
    // // 银行所在省
    // dto.put("prov_id", merchantAccessCardDO.getProvinceCode());
    // // 银行所在市
    // dto.put("area_id", merchantAccessCardDO.getCityCode());
    // // 银行编号参考： [银行编码](https://paas.huifu.com/partners/api/#/csfl/api_csfl_yhbm) ；
    // // &lt;font color&#x3D;&quot;green&quot;&gt;示例值：01020000 &lt;/font&gt;
    // // &lt;br/&gt;当card_type&#x3D;0时必填， 当card_type&#x3D;1或2时非必填
    // dto.put("bank_code", merchantAccessCardDO.getBankCode());
    // // 联行号参考：[银行支行编码](https://paas.huifu.com/partners/api/#/csfl/api_csfl_yhzhbm)
    // // 当card_type&#x3D;0时必填， 当card_type&#x3D;1或2时非必填 &lt;br/&gt;&lt;font
    // // color&#x3D;&quot;green&quot;&gt;示例值：102290026507&lt;/font&gt;
    // dto.put("branch_code", merchantAccessCardDO.getBranchCode());
    // // 支行名称开户支行名称。 当card_type&#x3D;0时必填， 当card_type&#x3D;1或2时非必填；
    // // &lt;br/&gt;&lt;font
    // // color&#x3D;&quot;green&quot;&gt;示例值：中国工商银行上海市中山北路支行&lt;/font&gt;
    // dto.put("branch_name", merchantAccessCardDO.getName());
    // //
    // 持卡人证件类型参见《[自然人证件类型](https://paas.huifu.com/partners/api/#/api_ggcsbm?id&#x3D;%e8%87%aa%e7%84%b6%e4%ba%ba%e8%af%81%e4%bb%b6%e7%b1%bb%e5%9e%8b)》
    // // 当card_type&#x3D;0时为空， 当card_type&#x3D;1或2时必填；&lt;font
    // // color&#x3D;&quot;green&quot;&gt;示例值：00&lt;/font&gt;
    // dto.put("cert_type", merchantAccessCardDO.getCertType());
    // // 持卡人证件有效期截止日期日期格式：yyyyMMdd，以北京时间为准。&lt;font
    // //
    // color&#x3D;&quot;green&quot;&gt;示例值：20220125&lt;/font&gt;，当cert_validity_type&#x3D;0时必填；当cert_validity_type&#x3D;1时为空
    // dto.put("cert_end_date",
    // HuiFuUtil.date(merchantAccessCardDO.getCertEndDate()));
    // // 持卡人证件号码
    // dto.put("cert_no", merchantAccessCardDO.getCertNo());
    // // 持卡人证件有效期类型
    // dto.put("cert_validity_type", merchantAccessCardDO.getCertValidityType());
    // // 持卡人证件有效期开始
    // dto.put("cert_begin_date",
    // HuiFuUtil.date(merchantAccessCardDO.getCertBeginDate()));
    // // 银行卡绑定手机号
    // dto.put("mp", merchantAccessCardDO.getCardPhone());
    //
    // return dto.toJSONString();
    // }

    /**
     * 新增文件参数转换
     * 
     * @param channelMerchantAccessContext
     */
    public static void addProcessFileId(ChannelMerchantAccessContext channelMerchantAccessContext) {
        MerchantAccessDto merchantAccessDto = channelMerchantAccessContext.getMerchantAccessDto();
        MerchantAccessLicenseDO merchantAccessLicense = merchantAccessDto.getMerchantAccessLicense();
        MerchantAccessCertInfoDO merchantAccessLegal = merchantAccessDto.getMerchantAccessLegal();
        MerchantAccessCardDO merchantAccessCard = merchantAccessDto.getMerchantAccessCard();
        PayChannelRequestDO beforeMerchantAccessRequestDO = channelMerchantAccessContext.getBeforePayChannelRequestDO();
        MerchantAccessBusinessDO merchantAccessBusiness = merchantAccessDto.getMerchantAccessBusiness();
        MerchantAccessFile merchantAccessFile = new MerchantAccessFile();
        Map<String, String> fileMap = new HashMap<>();
        if (beforeMerchantAccessRequestDO != null && StringUtils.hasText(beforeMerchantAccessRequestDO.getFileParams())) {
            fileMap = JsonUtils.parseObject(beforeMerchantAccessRequestDO.getFileParams(), Map.class);
        }
        try {
            // 营业执照
            CommonResult<String> f07 = fileUploadProcess(fileMap, merchantAccessLicense.getLicenseUrl(), "营业执照", "F07");
            if (f07.isSuccess()) {
                merchantAccessFile.setLicensePicFileId(f07.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f07.getMsg());
            }
            // 法人身份证人像面
            CommonResult<String> f02 = fileUploadProcess(fileMap, merchantAccessLegal.getCertFrontUrl(), "法人身份证人像面", "F02");
            if (f02.isSuccess()) {
                merchantAccessFile.setLegalCertFrontFileId(f02.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f02.getMsg());
            }
            // 法人身份证国徽面
            CommonResult<String> f03 = fileUploadProcess(fileMap, merchantAccessLegal.getCertBackUrl(), "法人身份证国徽面", "F03");
            if (f03.isSuccess()) {
                merchantAccessFile.setLegalCertBackFileId(f03.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f03.getMsg());
            }
            // 开户许可证
            CommonResult<String> f08 = fileUploadProcess(fileMap, merchantAccessCard.getLicenseUrl(), "开户许可证", "F08");
            if (f08.isSuccess()) {
                merchantAccessFile.setRegAcctFileId(f08.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f08.getMsg());
            }
            // 结算卡
            CommonResult<String> f13 = fileUploadProcess(fileMap, merchantAccessCard.getSettleCardFrontUrl(), "结算银行卡", "F13");
            if (f13.isSuccess()) {
                merchantAccessFile.setSettleCardFrontFileId(f13.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f13.getMsg());
            }

            switch (MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType())) {
                case PRIVATE_CORPORATE_ACCOUNT:
                    CommonResult<String> f24 = fileUploadProcess(fileMap, merchantAccessLegal.getCertFrontUrl(), "结算人身份证人像面", "F56");
                    if (f24.isSuccess()) {
                        merchantAccessFile.setSettleCertFrontFileId(f24.getData());
                    }
                    else {
                        channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                        channelMerchantAccessContext.setRemark(f24.getMsg());
                    }
                    CommonResult<String> f105 = fileUploadProcess(fileMap, merchantAccessLegal.getCertBackUrl(), "结算人身份证国徽面", "F55");
                    if (f105.isSuccess()) {
                        merchantAccessFile.setSettleCertBackFileId(f105.getData());
                    }
                    else {
                        channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                        channelMerchantAccessContext.setRemark(f105.getMsg());
                    }
                    break;
                default:
                    // 结算卡 结算人身份证人像面
                    CommonResult<String> f56 = fileUploadProcess(fileMap, merchantAccessCard.getCertFrontUrl(), "结算人身份证人像面", "F56");
                    if (f56.isSuccess()) {
                        merchantAccessFile.setSettleCertFrontFileId(f56.getData());
                    }
                    else {
                        channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                        channelMerchantAccessContext.setRemark(f56.getMsg());
                    }
                    // 结算卡 结算人身份证国会面
                    CommonResult<String> f55 = fileUploadProcess(fileMap, merchantAccessCard.getCertBackUrl(), "结算人身份证国徽面", "F55");
                    if (f55.isSuccess()) {
                        merchantAccessFile.setSettleCertBackFileId(f55.getData());
                    }
                    else {
                        channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                        channelMerchantAccessContext.setRemark(f55.getMsg());
                    }
            }

            // 授权委托书
            CommonResult<String> f15 = fileUploadProcess(fileMap, merchantAccessCard.getAuthUrl(), "对私非法人授权函", "F15");
            if (f15.isSuccess()) {
                merchantAccessFile.setAuthEnturstFileId(f15.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f15.getMsg());
            }
            // 线下经营-店铺门头照
            CommonResult<String> f22 = fileUploadProcess(fileMap, merchantAccessBusiness.getStoreHeaderUrl(), "门头照", "F22");
            if (f22.isSuccess()) {
                merchantAccessFile.setStoreHeaderFileId(f22.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f22.getMsg());
            }
            // 线下经营-店铺内景/工作区域照
            CommonResult<String> f24 = fileUploadProcess(fileMap, merchantAccessBusiness.getStoreIndoorUrl(), "店铺内景照", "F24");
            if (f24.isSuccess()) {
                merchantAccessFile.setStoreIndoorFileId(f24.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f24.getMsg());
            }
            // 线下经营-店铺内景/工作区域照
            CommonResult<String> f105 = fileUploadProcess(fileMap, merchantAccessBusiness.getStoreCashierDeskUrl(), "收银台", "F105");
            if (f105.isSuccess()) {
                merchantAccessFile.setStoreCashierDeskFileId(f105.getData());
            }
            else {
                channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantAccessContext.setRemark(f105.getMsg());
            }

            // // 线上经营-公司前台 照片id
            // CommonResult<String> f478 = fileUploadProcess(fileMap,
            // merchantAccessBusiness.getCompanyUrl(), "F478");
            // if (f478.isSuccess()) {
            // channelMerchantAccessContext.setOnlineCompanyFileId(f478.getData());
            // }
            // else {
            // channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessContext.setRemark(f478.getMsg());
            // }
            // // 线上经营-工作区域 照片id
            // CommonResult<String> f371 = fileUploadProcess(fileMap,
            // merchantAccessBusiness.getCompanyAreaUrl(), "F371");
            // if (f371.isSuccess()) {
            // channelMerchantAccessContext.setOnlineCompanyAreaFileId(f371.getData());
            // }
            // else {
            // channelMerchantAccessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessContext.setRemark(f371.getMsg());
            // }
            channelMerchantAccessContext.setMerchantAccessFile(merchantAccessFile);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        finally {
            PayChannelRequestDO payChannelRequestDO = channelMerchantAccessContext.getPayChannelRequestDO();
            payChannelRequestDO.setFileParams(JsonUtils.toJsonString(fileMap));
        }
    }

    public static void main(String[] args) {
        System.out.println(HuiFuUtil.date("202-02-23"));
        // System.out.println(file("https://www.hlkj.com"));
    }
}
